package com.betterwood.dragon.utils;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;

import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class TimeUtil {
    public static String currentTime() {
        LocalDateTime dateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return dateTime.format(formatter);
    }

    public static String timeStamp() {
        LocalDateTime dateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        return dateTime.format(formatter);
    }

    public static String hourMinuteStr() {
        LocalDateTime dateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HHmm");
        return dateTime.format(formatter);
    }

    public static String dateStr() {
        LocalDateTime dateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        return dateTime.format(formatter);
    }

    public static String toDateTimeStr(LocalDateTime dateTime) {
        if (dateTime == null) {
            return StringUtils.EMPTY;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return dateTime.format(formatter);
    }


    /**
     * 当前时间是否在3分钟之内
     * @param updateDate
     */
    public static boolean hasTimeCric(Date updateDate,Integer min) {
        Calendar c1=Calendar.getInstance();
        Calendar c2=Calendar.getInstance();
        Calendar c3=Calendar.getInstance();
        c1.setTime(updateDate);//要判断的日期
        c2.setTime(new Date());//初始日期
        c3.setTime(new Date());//也给初始日期 把分钟加3
        c3.add(Calendar.MINUTE, min);
        c2.add(Calendar.MINUTE,-min);//减去3分钟
        //System.out.println("c1"+c1.getTime());
        //System.out.println("c2"+c2.getTime());
        //System.out.println("c3"+c3.getTime());
        if(c1.after(c2)&&c1.before(c3)){
            //System.out.println("3分钟之内");
            return true;
        }else {
            //System.out.println("3分钟之外");
            return false;
        }
    }

    public static LocalDateTime beginOfToday() {
        return LocalDateTimeUtil.beginOfDay(LocalDateTime.now());
    }

    public static LocalDateTime endOfToday() {
        return LocalDateTimeUtil.endOfDay(LocalDateTime.now());
    }
}
